// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2023-11-08 10:11:31
// 生成路径: internal/app/plugin/mail/logic/plugin_mail.go
// 生成人：gfast
// desc:邮件发送
// company:云南奇讯科技有限公司
// ==========================================================================

package logic

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/tiger1103/gfast/v3/internal/app/plugins/mail/dao"
	"github.com/tiger1103/gfast/v3/internal/app/plugins/mail/model"
	"github.com/tiger1103/gfast/v3/internal/app/plugins/mail/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/plugins/mail/service"
	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
	"github.com/tiger1103/gfast/v3/library/liberr"
	"gopkg.in/gomail.v2"
)

const (
	MailConfigKey = "MailConfigKey"
)

func init() {
	service.RegisterPluginMail(New())
}

func New() service.IPluginMail {
	return &sPluginMail{}
}

type sPluginMail struct{}

func (s *sPluginMail) List(ctx context.Context, req *model.PluginMailSearchReq) (listRes *model.PluginMailSearchRes, err error) {
	listRes = new(model.PluginMailSearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.PluginMail.Ctx(ctx).WithAll()

		if req.MType != "" {
			m = m.Where(dao.PluginMail.Columns().MType+" = ?", req.MType)
		}
		if req.User != "" {
			m = m.Where(dao.PluginMail.Columns().User+" = ?", req.User)
		}
		if req.Host != "" {
			m = m.Where(dao.PluginMail.Columns().Host+" = ?", req.Host)
		}
		if req.Port != "" {
			m = m.Where(dao.PluginMail.Columns().Port+" = ?", gconv.Int(req.Port))
		}
		if req.Status != "" {
			m = m.Where(dao.PluginMail.Columns().Status+" = ?", gconv.Int(req.Status))
		}
		listRes.Total, err = m.Count()
		liberr.ErrIsNil(ctx, err, "获取总行数失败")
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		listRes.CurrentPage = req.PageNum
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id asc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		var res []*model.PluginMailListRes
		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
		listRes.List = make([]*model.PluginMailListRes, len(res))
		for k, v := range res {
			listRes.List[k] = &model.PluginMailListRes{
				Id:     v.Id,
				MType:  v.MType,
				User:   v.User,
				Pass:   v.Pass,
				Host:   v.Host,
				Port:   v.Port,
				Status: v.Status,
				Remark: v.Remark,
			}
		}
	})
	return
}

func (s *sPluginMail) GetById(ctx context.Context, id uint) (res *model.PluginMailInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.PluginMail.Ctx(ctx).WithAll().Where(dao.PluginMail.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
	})
	return
}

func (s *sPluginMail) Add(ctx context.Context, req *model.PluginMailAddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.PluginMail.Ctx(ctx).Insert(do.PluginMail{
			MType:  req.MType,
			User:   req.User,
			Pass:   req.Pass,
			Host:   req.Host,
			Port:   req.Port,
			Status: req.Status,
			Remark: req.Remark,
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sPluginMail) Edit(ctx context.Context, req *model.PluginMailEditReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.PluginMail.Ctx(ctx).WherePri(req.Id).Update(do.PluginMail{
			MType:  req.MType,
			User:   req.User,
			Pass:   req.Pass,
			Host:   req.Host,
			Port:   req.Port,
			Status: req.Status,
			Remark: req.Remark,
		})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}

func (s *sPluginMail) Delete(ctx context.Context, ids []uint) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.PluginMail.Ctx(ctx).Delete(dao.PluginMail.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}

// SendData 发送邮件内容
func (s *sPluginMail) SendData(ctx context.Context, req *model.MailReq) (err error) {
	var configParam *model.PluginMailInfoRes
	err = dao.PluginMail.Ctx(ctx).Where(dao.PluginMail.Columns().MType, req.MType).Scan(&configParam)
	if err != nil {
		return
	}
	gm := gomail.NewDialer(configParam.Host, configParam.Port, configParam.User, configParam.Pass)
	m := gomail.NewMessage()
	m.SetHeader("From", "gfast-Email"+"<"+configParam.User+">")
	m.SetHeader("To", req.MailTo...)    //发送给多个用户
	m.SetHeader("Subject", req.Subject) //设置邮件主题
	m.SetBody("text/html", req.Body)    //设置邮件正文
	return gm.DialAndSend(m)
}
